﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Xml;
using System.Xml.Serialization;

namespace dkProfiler
{
    public class LevelValues
    {
        [XmlAttribute()]
        public int Level { get; set; }

        public RaceValues[] ArrayOfRaceValues { get; set; }
    }

    public class RaceValues
    {
        [XmlAttribute()]
        public int Race { get; set; }

        public int Health { get; set; }
        public int Strength { get; set; }
        public int Agility { get; set; }
        public int Stamina { get; set; }
        public int Intellect { get; set; }
        public int Spirit { get; set; }
    }

    public class BaseStatSource
    {
        LevelValues[] levelValues;

        public BaseStatSource()
        {
            levelValues = new LevelValues[1];
            levelValues[0] = new LevelValues();
            levelValues[0].Level = 80;
            levelValues[0].ArrayOfRaceValues = new RaceValues[1];
            levelValues[0].ArrayOfRaceValues[0] = new RaceValues();
            levelValues[0].ArrayOfRaceValues[0].Race = 0;
        }

        public BaseStatSource(string fileName)
        {
            XmlSerializer xs = new XmlSerializer(typeof(LevelValues[]));
            try
            {
                using (FileStream fs = new FileStream(fileName, FileMode.Open))
                    levelValues = xs.Deserialize(fs) as LevelValues[];
            }
            catch (Exception e)
            {
                if (e is InvalidOperationException)
                    MessageBox.Show("baseStats.xml contains invalid data.", "Invalid data file", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        public void Save(string fileName)
        {
            XmlSerializer xs = new XmlSerializer(typeof(LevelValues[]));
            using (FileStream fs = new FileStream(fileName, FileMode.Create))
                xs.Serialize(fs, levelValues);
        }

        public int GetBaseStat(int level, int race, string stat)
        {
            RaceValues r = levelValues.Where(x => x.Level == level).First<LevelValues>().ArrayOfRaceValues.Where(x => x.Race == race).First<RaceValues>();
            PropertyInfo pi = r.GetType().GetProperty(stat);
            return (int)pi.GetValue(r, null);
        }
    }
}